<template>
  <div>
    <el-button @click="addToMaterial" style="margin-left: 16px;">
      添加物料
    </el-button>
    <!--  模糊查询   -->
    <el-form :inline="true" :model="formDate" class="demo-form-inline">
      <el-form-item label="">
        <el-input v-model="formDate.name" placeholder="物料名称"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input v-model="formDate.materialCode" placeholder="物料编号"></el-input>
      </el-form-item>
      <el-date-picker
          v-model="formDate.time"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
      </el-date-picker>
      <el-form-item>
        <el-button type="primary" @click="getMaterialPageAll">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 页面展示 -->
    <el-table
        :data="tableData"
        stripe
        style="width: 100%">
      <el-table-column
          prop="name"
          label="物料名称">
      </el-table-column>
      <el-table-column
          prop="status"
          label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status===0">正常</el-tag>
          <el-tag type="danger" v-if="scope.row.status===1">异常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="pic"
          label="主图">
        <template slot-scope="scope">
          <el-image :src="'http://10.39.43.34:8084/'+scope.row.pic" style="width: 50%"></el-image>

        </template>
      </el-table-column>
      <el-table-column
          prop="materialCode"
          label="物料编号">
      </el-table-column>
      <el-table-column
          prop="propertyGroup"
          label="属性组合">
      </el-table-column>
      <el-table-column
          prop="simpleName"
          label="简称">
      </el-table-column>
      <el-table-column
          prop="unit"
          label="单位">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.unit===0">一件</el-tag>
          <el-tag v-if="scope.row.unit===1">一箱</el-tag>
          <el-tag v-if="scope.row.unit===2">一包</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="standard"
          label="非/标准">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.standard===0">标准</el-tag>
          <el-tag v-if="scope.row.standard===1">非标准</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="froms"
          label="来源">
      </el-table-column>
      <el-table-column
          prop="materialNature"
          label="物料性质">
      </el-table-column>
      <el-table-column
          prop="materialCharacter"
          label="物料特性">
      </el-table-column>
      <el-table-column
          prop="updateTime"
          label="添加时间">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="120">
        <template slot-scope="scope">
          <el-button
              @click.native.prevent="updateToMaterial(scope.row)"
              type="text"
              size="small">
            修改物料
          </el-button>
          <el-button
              @click.native.prevent="deleteRow(scope.row)"
              type="text"
              size="small">
            移除物料
          </el-button>
        </template>
      </el-table-column>


    </el-table>
    <div class="block">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[3, 4, 5, 6]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!-- 添加操作  -->
    <!-- Form -->
    <el-drawer
        :visible.sync="dialogFormVisible"
        :with-header="false"
        size="50%">
      <div>
        <el-form :model="form">
          <el-form-item label="">
            <el-input v-model="form.name" placeholder="物料名称" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-select
                v-model="form.materialCode"
                filterable
                remote
                reserve-keyword
                placeholder="物料编号:自定义/搜索"
                :remote-method="remoteMethod"
                :loading="loading">
              <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="物料状态">
            <el-radio-group v-model="form.status">
              <el-radio :label="0">正常</el-radio>
              <el-radio :label="1">异常</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="主图">
            <el-upload
                class="upload-demo"
                action="http://10.39.43.34:8084/material/addImg"
                :on-success="addImg"
                list-type="picture">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-form-item>
          <el-form-item label="">
            <el-input v-model="form.propertyGroup" placeholder="属性组合" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="单位">
            <el-radio-group v-model="form.unit">
              <el-radio :label="0">一件</el-radio>
              <el-radio :label="1">一箱</el-radio>
              <el-radio :label="2">一包</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="非/标准">
            <el-radio-group v-model="form.standard">
              <el-radio :label="0">标准</el-radio>
              <el-radio :label="1">非标准</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="">
            <el-input v-model="form.simpleName" placeholder="物料简称"  autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-input v-model="form.froms" placeholder="来源-数字"  autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-input v-model="form.materialNature" placeholder="物料性质"  autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-input v-model="form.materialCharacter" placeholder="物料特性"  autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addOrUpdateMaterial">确 定</el-button>
        </div>
      </div>

    </el-drawer>
  </div>
</template>
<script>
export default {
  name: "List",
  data() {
    return {
      drawer: false,
      tableData: [],
      form: {},
      dialogFormVisible: false,
      formDate: {},
      pageSize: 3,
      pageNum: 1,
      total: 100,
      // 搜索编号
      options: [],
      list: [],
      loading: false,
      states: []

    }
  }, methods: {
    //获取编号
    getCode() {
      this.axios.post("http://10.39.43.34:8084/iso/getCode/").then(res => {
        this.states = res.data
        this.list = this.states.map(item => {
          return {value: `${item}`, label: `${item}`};
        });

      })
    },
    // 获取编号
    remoteMethod(query) {
      if (query !== '') {
        this.loading = true;
        setTimeout(() => {
          this.loading = false;
          this.options = this.list.filter(item => {
            return item.label.toLowerCase()
                .indexOf(query.toLowerCase()) > -1;
          });
        }, 200);
      } else {
        this.options = [];
      }
    },
    //修改物料
    updateToMaterial(row) {
      this.form = row;
      this.dialogFormVisible = true;
    },
    // 删除指定的物料
    deleteRow(row) {
      console.info(row)
      this.axios.post("http://10.39.43.34:8084/material/delMaterial/" + row.id).then(res => {
        if (res) {
          alert("移除成功")
          this.getMaterialPageAll();
        } else {
          alert("移除失败")
        }
      })

    },
    // 去添加页面
    addToMaterial() {
      this.form = {};
      this.options = [];
      this.dialogFormVisible = true;
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getMaterialPageAll();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getMaterialPageAll();
    },
    // 上传图片
    addImg(response) {
      this.form.pic = response;
    },
    //列表展示
    getMaterialPageAll() {
      this.axios.post("http://10.39.43.34:8084/material/getMaterialPageAll/" + this.pageNum + "/" + this.pageSize, this.formDate).then(res => {
        console.info(res)
        this.total = res.data.total
        this.tableData = res.data.rows;
      })
    },
    //添加操作
    addOrUpdateMaterial() {
      this.dialogFormVisible = false;
      this.axios.post("http://10.39.43.34:8084/material/addOrUpdateMaterial", this.form).then(res => {
        if (res) {
          alert("操作成功")
          this.getMaterialPageAll();
        } else {
          alert("操作失败")
        }
      })
    }
  }, created() {
    this.getCode();
    this.getMaterialPageAll();
  }

}
</script>
<style scoped>
</style>